package com.itheima.leetcode.od.c.logicalsimulation;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * <h3>反转每对括号间的子串</h3>
 */
public class ReverseSubstringBetweenParentheses {
    public static void main(String[] args) {
        //输入
        Scanner in = new Scanner(System.in);
        String input_str = in.nextLine();

        System.out.println(reverseParentheses(input_str));
    }

    public static String reverseParentheses(String s) {
        Deque<String> stack = new LinkedList<String>();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(') {
                stack.push(sb.toString());
                sb.setLength(0);
            } else if (ch == ')') {
                sb.reverse();
                sb.insert(0, stack.pop()); // 头插pop()
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }
}
